<?php
include("../../tools/session.php");

// 如果登录成功
if ($is_login_success === true)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="utf-8">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="Content-Language" content="UTF-8" />
  <meta content="all" name="robots" />
  <meta name="author" content="tiger"/>
  <meta name="Copyright" content="jinzhou group" />
  <meta name="description" content="jinzhou group oa" />
  <title>生成公司－部门－职位数组</title>
  <script language="javascript">
    function produce()
    {
	   window.location = "produce_company_position.php?opt=1";
	}
  </script>
</head>
<body>
<?php
// 加载路径数据
require_once("filepaths.php");

$opt = $_GET["opt"];
if (!$opt)
{
?>
  <input type="button" onclick="produce();" value="生成" />
<?php
  exit();
}

	include("../../tools/mysql.php");
	db_connect();
   
	$company = array();
   
	// $sql = "SELECT * FROM view_comp_sec_pos WHERE position_id<>0 AND section_id<>0 AND company_id<>0 ORDER BY position_id ASC";
	$sql = "SELECT a.company_id company_id, a.zh_name company_name, a.power company_power,
				b.section_id section_id, b.zh_name section_name, b.master_id master_id, b.power section_power, 
				c.position_id position_id, c.zh_name position_name, c.power position_power
			FROM company a, section b, position c
			WHERE a.company_id = b.company_id AND b.section_id = c.section_id
					 AND a.company_id<>0 AND b.section_id<>0 AND c.position_id<>0
			ORDER BY company_id, section_id, position_id ASC";
	$re = db_query($sql);
   
	foreach ($re as $v)
	{
		$c_id			= $v["company_id"];
		$c_name		= $v["company_name"];
		$c_power		= $v["company_power"];
		$s_id			= $v["section_id"];
		$s_name		= $v["section_name"];
		$s_power		= $v["section_power"];
		$s_masterid	= $v["master_id"];
		$p_id			= $v["position_id"];
		$p_name		= $v["position_name"];
		$p_power		= $v["position_power"];
		
		// $c_temp 是一条“公司”的数组信息，其中第 [0] 项为指向“部门”数组的数组
		// $s_temp 是一条“部门”的数组信息，其中第 [0] 项为指向“职位”数组的数组
		// $p_temp 是一条“职位”的数组信息
		$c_temp = array(array(), $c_name, $c_power);
		$s_temp = array(array(), $s_name, $s_power, $s_masterid);
		$p_temp = array($p_name, $p_power);
		
		if (!isset($company[$c_id])) { $company[$c_id] = $c_temp; }
		if (!isset($company[$c_id][0][$s_id])) { $company[$c_id][0][$s_id] = $s_temp; }
		if (!isset($company[$c_id][0][$s_id][0][$p_id])) { $company[$c_id][0][$s_id][0][$p_id] = $p_temp; }
	}
   
   	$hash_1 = array();
	foreach ($company as $k1 => $v1)
	{
		$str_1 = "'" . $k1 . "':[{";
		$hash_2 = array();
		foreach ($v1[0] as $k2 => $v2)
		{
			$str_2 = "'" . $k2 . "':[{";
			$hash_3 = array();
			foreach ($v2[0] as $k3 => $v3)
			{
				$str_3 = "'" . $k3 . "':['" . $v3[0] . "','" . $v3[1] . "']";
				$hash_3[] = $str_3;
			}
			$str_2 .= implode(",", $hash_3) . "},'" . $v2[1] . "','" . $v2[2] . "','" . $v2[3] . "']";
			$hash_2[] = $str_2;
		}
		$str_1 .= implode(",", $hash_2) . "},'" . $v1[1] . "','" . $v1[2] . "']";
		$hash_1[] = $str_1;
	}
	$last_str = "var __company_2_position__ = {" . implode(",", $hash_1) . "};";
	
	/* -------------- 将生成的数据写入文件 -------------- */
	$filename = "data_company_position.js";
	$handle = fopen($file_paths["produced"] . $filename, "w");
	
	if (!fwrite($handle, $last_str))
	{
		echo "生成数据文件 " . $file_paths["produced"] . $filename . " 失败！";
	}
	else 
	{
		echo "生成数据文件 " . $file_paths["produced"] . $filename . " 成功！";
		copy($file_paths["produced"] . $filename, $file_paths["data_js"] . $filename);
	}
   
	fclose($handle);
	
	// echo $last_str;
?>
<?php
}
// 如果登录失败或还未登录
else
{
   session_hop_page("../index.php");
   exit();
}
	  
?>
</body>
</html>
